#!/usr/bin/env/python3.7
# -*- coding: utf-8 -*-

"""
@File    : setting.py
@Time    : 12/04/2021
@Author  : Silence
@Version : 1.0
@Contact : silence2021silence@163.com
@License : (C)Copyright 2021-2021, Silence
@Desc    : None
"""


import pymysql

database = pymysql.connect(host="120.24.51.156", user="dormitory_system", password="123456",
                                   database="dormitory_system", charset="utf8")
class Setting():
    def setting(open_datetime, late_datetime, leaving_timeout, infrared_value, temperature_value_min,
                 temperature_value_max, weight_value_min, weight_value_max,
                 volume_value_max, somke_ttl, smoke_value_max):
        cursor_1 = database.cursor()
        sql_1 = """
                DELETE FROM `dormitory_system`.`dormitory_system_setting` 
                        """
        cursor_1.execute(sql_1)
        result_1 = cursor_1.fetchall()

        cursor_2 = database.cursor()
        sql_2 = """
                INSERT INTO `dormitory_system`.`dormitory_system_setting` (
	                `id`, `open_datetime`, `late_datetime`, 
	                `leaving_timeout`, `infrared_value`, 
	                `temperature_value_min`, `temperature_value_max`, 
	                `weight_value_min`, `weight_value_max`, 
	                `volume_value_max`, `smoke_ttl`, 
	                `smoke_value_max`
                ) 
                VALUES 
	            (
		            '%s', '%s', '%s', '%s', '%s', '%s', '%s', 
		            '%s', '%s', '%s', '%s', '%s'
	            )
                        """
        sql_3 = sql_2 % ('NULL', open_datetime, late_datetime, leaving_timeout,
                         infrared_value, temperature_value_min, temperature_value_max,
                         weight_value_min, weight_value_max, volume_value_max,
                         somke_ttl, smoke_value_max)
        cursor_2.execute(sql_3)
        database.commit()
        result_2 = cursor_2.fetchall()

    def read_setting():
        cursor = database.cursor(cursor=pymysql.cursors.DictCursor)
        sql = """
                select
        	        open_datetime, 
        	        late_datetime, 
        	        leaving_timeout, 
        	        infrared_value, 
        	        temperature_value_min, 
        	        temperature_value_max, 
        	        weight_value_min, 
        	        weight_value_max, 
        	        volume_value_max, 
        	        smoke_ttl, 
        	        smoke_value_max 
                from
        	        dormitory_system_setting

                """
        cursor.execute(sql)
        result = cursor.fetchall()
        if result == ():
            return ('none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'nane')
        else:
            for data in result:
                open_datetime = data["open_datetime"]
                late_datetime = data["late_datetime"]
                leaving_timeout = data["leaving_timeout"]
                infrared_value = data["infrared_value"]
                temperature_value_min = data["temperature_value_min"]
                temperature_value_max = data["temperature_value_max"]
                weight_value_min = data["weight_value_min"]
                weight_value_max = data["weight_value_max"]
                volume_value_max = data["volume_value_max"]
                smoke_ttl = data["smoke_ttl"]
                smoke_value_max = data["smoke_value_max"]

            return (open_datetime, late_datetime, leaving_timeout, infrared_value, temperature_value_min,
                    temperature_value_max, weight_value_min, weight_value_max, volume_value_max, smoke_ttl,
                    smoke_value_max)